package com.app.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.Application;
import com.app.R;
import com.app.constant.Constants;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.bean.Bike;
import com.app.model.request.BikeReq;
import com.app.model.response.ResObj;
import com.app.utils.LoginUtil;
import com.app.utils.StringUtil;
import com.app.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivitySharePwd extends ActivityBaseMenuImage {

    private static final String LOG_TAG = ActivitySharePwd.class.getSimpleName();
    @BindView(R.id.etNum)
    EditText etNum;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;

    private HttpDataLoader dataLoader;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_pwd);
        ButterKnife.bind(this);
        dataLoader = new HttpDataLoader(this);
        setTitle("分享解锁码");
//        setIvBackImageRes(R.mipmap.icon_left_arrow);
//        setIvBackClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });

        /**
         * 缩小编辑框隐藏字体大小
         */
        SpannableString ss1 = new SpannableString("输入车辆编号");//定义hint的值
        AbsoluteSizeSpan ass1 = new AbsoluteSizeSpan(20, true);//设置字体大小 true表示单位是sp
        ss1.setSpan(ass1, 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        etNum.setHint(new SpannedString(ss1));
        SpannableString ss2 = new SpannableString("输入解锁码");//定义hint的值
        AbsoluteSizeSpan ass2 = new AbsoluteSizeSpan(20, true);//设置字体大小 true表示单位是sp
        ss2.setSpan(ass2, 0, ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        etPwd.setHint(new SpannedString(ss2));
//        setMenuText("查询");
//        setOnMenuClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (etContent.getText() == null || etContent.getText().length() < 1) {
//                    ToastUtil.getInstance().showToast(ActivityGetPwd.this, "车辆编号不能为空，请重新输入");
//                } else {
//                    getPwd();
//                }
//            }
//        });
    }

    private void sharePwd() {
        if (StringUtil.isEmpty(etNum.getText().toString())) {
            ToastUtil.getInstance().showToast(this, "请输入车辆编号！");
            return;
        }
        if (StringUtil.isEmpty(etPwd.getText().toString())) {
            ToastUtil.getInstance().showToast(this, "请输入车辆密码！");
            return;
        }
        if (!LoginUtil.isLogin()) {
//            startActivity(new Intent(this, ActivityLogin.class));
            showLoginDialog();
            return;
        }
        BikeReq req = new BikeReq();
        Bike bike = new Bike();
        bike.setNum(etNum.getText().toString());
        bike.setPwd(etPwd.getText().toString());
        req.setBike(bike);
        dataLoader.POST(HttpDataReqUrl.URL_SHARE_PWD, req);
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("车辆编号", bike.getNum());
        map_value.put("车辆密码", bike.getPwd());
        MobclickAgent.onEventValue(Application.instance, "sharePwd" , map_value, 1);
    }
    private void showLoginDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(ActivitySharePwd.this);
        builder.setMessage("您尚未登录，请先登录！");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(ActivitySharePwd.this, ActivityLogin.class));

            }
        });
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
        builder.create().show();
    }


    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
        if (url.equals(HttpDataReqUrl.URL_SHARE_PWD)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof Bike) {

                }
                ToastUtil.getInstance().showToast(ActivitySharePwd.this, result.getMsg());
                finish();
            } else {
                ToastUtil.getInstance().showToast(ActivitySharePwd.this, result.getMsg());
            }
        }


    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        Log.e(LOG_TAG, "error: " + error);
        ToastUtil.getInstance().showToast(ActivitySharePwd.this, error);
    }

    @OnClick({R.id.tvSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvSubmit:
                sharePwd();
                break;
            default:
                break;

        }
    }
}
